package com.itheima.task;


import com.itheima.feign.MobileArticleClient;
import com.itheima.po.MediaNews;
import com.itheima.service.NewsService;
import com.itheima.vo.MediaNewsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;


/**
 * @author :wujunjie
 * createTime :2023-01-04 16:59
 * description :
 */

//@Component
@Slf4j
public class OnlineNews {
    @Autowired
    private NewsService newsService;

    //该参数接收一个cron表达式，cron表达式是一个字符串，字符串以5或6个空格隔开，分开共6或7个域，每一个域代表一个含义。
    //[秒] [分] [小时] [日] [月] [周] [年](年是非必填的)
    //指定一个时间点执行一次任务
    @Scheduled(fixedRate = 5*1000)
    public void task() {
       List<MediaNews> mediaNews=newsService.selectNewsByStatus(8);
       if (CollectionUtils.isEmpty(mediaNews)){
           throw new RuntimeException("暂无需要更新的数据");
       }
        for (MediaNews mediaNew : mediaNews) {
            Long id = mediaNew.getId();
            mediaNew.setId(null);
            MediaNews mediaNewsPublish = newsService.pubishArticle(mediaNew);
            mediaNewsPublish.setId(id);
            mediaNewsPublish.setStatus(9);
            Boolean update=newsService.updateById(mediaNewsPublish);
            if (!update){
                throw new RuntimeException("更新状态失败");
            }
        }
    }
}
